<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Data Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage Data  
 * @author Manh Nguyen - 06/08/2012 
 * @link http://sunsoft.vn
 */
class Data_model extends DataMapper {

    var $mode = 'data_model';
    var $table = 'data';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
        $this->partner_id = 'all';
        $this->validation = array(
            'type' => array(
                'values' => array(
                    //'background' => $this->lang->line('background'),
                    'screenshot' => $this->lang->line('screenshot'),
                    //'game' => $this->lang->line('game'),
                    'artwork' => $this->lang->line('artwork'),
                    //'item' => $this->lang->line('item'),
                    'wallpaper' => $this->lang->line('wallpaper'),
                    'download' => $this->lang->line('download'),
                    //'member' => $this->lang->line('avatar'),
                    'files' => $this->lang->line('files'),
                    'video' => $this->lang->line('video')
                    //'audio' => $this->lang->line('audio'),
                )
            )
        );
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------	
    var $validation = array();

    /**
     * Default order in query select 
     */
    var $default_order_by = array('created' => 'desc');

    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->file_name) ? '---empty---' : $this->file_name;
    }

    /**
     * string value of type.
     * 
     * @access public
     * @param null
     * @return string
     */
    function get_type() {
        return $this->validation['type']['values'][$this->type];
    }

    /**
     * array type of object
     * 
     * @access public
     * @param bool
     * @return array
     */
    function type_list($zero = FALSE) {
        $type_list = $this->validation['type']['values'];
        if ($zero) {
            $type_list = array_merge(array('' => '--' . $this->lang->line('all') . '--'), $type_list);
        }
        return $type_list;
    }

    /**
     * array partner of object
     *
     * @access public
     * @param bool
     * @return array
     */
    function partner_list($zero = FALSE) {
        //$partner_list = $this->validation['partner_id']['values'];

        $partner = new Partner_model();
        $partner->select('id,name');
        $result = $partner->get();
        $partner_list = array();
        foreach ($result as $item) {
            $partner_list[$item->id] = $item->name;
        }
        if ($zero) {
            $partner_list = array_merge(array('' => '--' . $this->lang->line('all') . '--'), $partner_list);
        }
        return $partner_list;
    }

    /**
     * string of list data type alowe to upload.
     * 
     * @access public
     * @param void
     * @return string
     */
    function allowed_types() {
        return 'pdf|doc|docx|xls|xlsx|zip|rar|7z|jpg|gif|png|jpeg|txt|exe';
    }

    /**
     * string url of data.
     * 
     * @access public
     * @param void
     * @return string
     */
    function get_url() {
        if ($this->id <= 0)
            return '#';
        return base_url() . UPLOADFILEDIR . $this->type . '/' . $this->file_name;
    }

    /**
     * check exist file in server
     * 
     * @access public
     * @param void
     * @return bool
     */
    function check_exists() {
        if ($this->id > 0) {
            return is_file($this->upload_dir() . $this->file_name);
        }
        return FALSE;
    }

    /**
     * path of dir to upload file.
     * 
     * @access public
     * @param void
     * @return string
     */
    function upload_dir() {
        $dir = FCPATH . UPLOADFILEDIR . $this->type;
        if (!is_dir($dir)) {
            mkdir($dir);
        }
        return $dir . '/';
    }

    /**
     * rewite delete function to delete file before delete data.
     * 
     * @access public
     * @param obj, string
     * @return bool
     */
    function delete($object = '', $related_field = '') {
        if (empty($object) && !is_array($object) && $this->id > 0) {
            $full_path = $this->upload_dir() . $this->file_name;
            if (is_file($full_path)) {
                unlink($full_path);
            }
        }
        return parent::delete($object, $related_field);
    }

    function extra_path($path) {
        if ($this->extra_type($path) == 'img')
            return "<img src='" . base_url() . $path . "' height='128px'>";
        else
            return "<img src='" . base_url() . 'data/images/icons/' . $this->extra_type($path) . ".png'>";
    }

    function extra_type($path) {
        $extension = end(explode('.', $path));
        $extensions = array(
            'download' => array('zip', 'rar', '7z', 'exe', 'txt'),
            'img' => array("jpg", "gif", "png", "jpeg"),
            'pdf' => array('pdf'),
            'doc' => array('doc', 'docx'),
            'xls' => array('xls', 'xlsx')
        );

        foreach ($extensions as $type => $ext_arr) {
            if (in_array($extension, $ext_arr))
                return $type;
        }
    }

    /**
     * array metadata for generate jquery grid. 
     * 
     * @access public
     * @param null
     * @return array
     */
    function jqgrid_metadata() {
        //edittype: 'text', 'textarea', 'select', 'checkbox', 'password', 'button', 'image', 'file', 'custom' 
        return array(
            'names' => array(
                'id',
                $this->lang->line('title'),
                $this->lang->line('description'),
                $this->localize_label('name'),
                $this->localize_label('path'),
                $this->localize_label('type'),
                $this->localize_label('created'),
                $this->localize_label('download')
            ),
            'model' => array(
                array('name' => 'id', 'hidden' => TRUE, 'key' => TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'description',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'name',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'path',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'type',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->type_list(TRUE)
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'created',
                    'search' => FALSE
                ),
                array('name' => 'hitcount',
                    'searchoptions' => array(
                        'sopt' => array('eq', 'gt', 'ge', 'lt', 'le')
                    )
                )
            )
        );
    }

    /**
     * increase download number of the data
     * 
     * @access public
     * @param null
     * @return bool
     */
    function increase_download() {
        $data = new Data_model();
        $data->where('code', 'client-game')->get();
        $data->hitcount = $data->hitcount + 1;
        $data->save();
        $data->check_last_query();
        

    
    }

    /**
     * get download number of the data
     * 
     * @access public
     * @param null
     * @return int
     */
    function get_download() {
        $this->select('hitcount');
        $this->where('code', 'client-game');
        return  $this->get();

        //return intval($download->num);

    }

    /**
     * get path file of the data
     * 
     * @access public
     * @param null
     * @return array
     */
    function get_by_code($code) {
        $this->select('path');
        $this->where('code', $code);
        return  $this->get();
    }
    /**
     * get path image of the data
     * 
     * @access public
     * @param null
     * @return array
     */
    function get_by_type($type = NULL,$limit = 6, $offset = 0) {
        $this->select('title, path, created');
        $this->where('type', $type);
        $this->get($limit, $offset);
        return  $this;
    }    

    function statistics_by_type(){
    	$this->select("count(id) as count, type");
    	$this->group_by("type");
    	$this->get();
    	
    	$return = array();
    	foreach($this as $item){
    		if($item->count>0){
    			$return[$item->type]		= $item->count;
    		}    		
    	}
    	return $return;
    }
}

/* End of file data.php */
/* Location: ./application/models/data.php */
